<template>
  <view>
    <view class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
      <button type="primary" size="mini" @click="chooseAddress">请选择您的地址</button>
    </view>
    <view class="address-info-box" v-else @click="chooseAddress">
      <view class="row1">
        <view class="left-box">
          <text>收货人:</text>
          <text class="add-name">{{ address.userName }}</text>
        </view>
        <view class="right-box">
          <text>电话:</text>
          <text class="phone">{{ address.telNumber }}</text>
          <uni-icons type="arrowright"></uni-icons>
        </view>
      </view>
      <view class="row2">
        <text class="add-con-title">收获地址:</text>
        <text class="add-con">{{addstr}}</text>
      </view>
    </view>

    <image class="address-border" src="@/static/cart_border@2x.png" mode=""></image>
  </view>
</template>

<script>
import {mapMutations,mapState,mapGetters} from 'vuex'
export default {
  name: 'my-address',
  data() {
    return {
      // address: {}
    };
  },
  computed: {
    ...mapState('m_user',['address']),
    ...mapGetters('m_user',['addstr'])
    // 将addstr--地址详细抽离到vuex getters中，提高复用
    // addstr() {
    //   if (!this.address.provinceName) return '';
    //   return this.address.provinceName + this.address.cityName + this.address.countyName + this.address.detailInfo;
    // },
  },
  methods: {
    ...mapMutations('m_user',['updateUserAddress']),
    async chooseAddress() {
      const [err, suc] = await uni.chooseAddress().catch((err) => err);
      if (err === null && suc.errMsg === 'chooseAddress:ok') {
        console.log(suc);
        // this.address = suc;
        this.updateUserAddress(suc)
        
      }
    }
  }
};
</script>

<style lang="scss">
.address-choose-box {
  height: 90px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.address-info-box {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 90px;
  padding: 0 10px;
  font-size: 13px;
  .row1 {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    .left-box {
      .add-name {
        padding-left: 1px;
      }
    }
    .right-box {
      .phone {
        padding-left: 1px;
      }
    }
  }
  .row2 {
    // margin-top: 10px;
    display: flex;
    // justify-content: space-between;
    align-items: center;

    .add-con-title {
      white-space: nowrap;
    }
    .add-con {
      padding-left: 10px;
      overflow: hidden; /* 隐藏超出的内容 */
      display: -webkit-box; /* 使用Webkit盒模型 */
      -webkit-line-clamp: 2; /* 限制为2行 */
      -webkit-box-orient: vertical; /* 文字垂直排列 */
    }
  }
}
.address-border {
  display: block;
  width: 100%;
  height: 5px;
}
</style>
